//
//  WmcMediaTeamAlbumAPI.swift
//  YPWatermarkCamera
//
//  Created by 周诗松 on 2022/9/15.
//

import Foundation
import RxSwift
import YPNetwork

struct WmcMediaTeamAlbumAPI {
    
    /// 获取相册资源最大id
    /// - Parameter businessId: ???
    /// - Returns: Observable<YPNetworkResponse>
    static func fetchAlbumMaxResourceId(businessId: Int? = nil) -> Observable<YPNetworkResponse> {
        var params: [String: Any] = [:]
        if let businessId = businessId {
            params["busId"] = businessId
        }
        return WmcMediaRequest.requestData(path: "/wmc/v1/teamAlbum/getMaxAlbumResourceId", parameters: params) { request in
            request.method = .post
        }
    }
    
    /// 获取相册资源最大id
    /// - DocumentUrl: http://yapi.3pvr.com/project/219/interface/api/3351
    /// - Returns: Observable<YPNetworkResponse>
    static func fetchAlbumMaxResourceId() -> Observable<YPNetworkResponse> {
        return WmcMediaRequest.requestData(path: "/wmc/v1/teamAlbum/editSyncConfig") { request in
            request.method = .post
        }
    }
    
    /// 相册列表
    /// - DocumentUrl: http://yapi.3pvr.com/project/219/interface/api/3041
    /// - Returns: Observable<[WmcMediaTeamAlbumModel]>
    static func fetchTeamAlbumList() -> Observable<[WmcMediaTeamAlbumModel]> {
        return WmcMediaRequest.requestData(path: "/wmc/v1/teamAlbum/teamAlbumList") { request in
            request.method = .post
        }.parseListModelWithClass(WmcMediaTeamAlbumModel.self)
    }
    
    /// 分类相册列表
    /// - DocumentUrl: http://yapi.3pvr.com/project/219/interface/api/3066
    /// - Parameters:
    ///   - albumId: 相册id
    ///   - resourceType: 资源类型集合
    ///   - teamId: 团队id
    /// - Returns: Observable<[WmcMediaTeamAlbumModel]>
    static func fetchClassifyAlbumList(albumId: String, resourceType: [Int]? = nil, teamId: Int? = nil) -> Observable<[WmcMediaAlbumModel]> {
        var params: [String: Any] = ["albumId": albumId]

        if let resourceType = resourceType {
            params["resourceType"] = resourceType
        }
        
        if let teamId = teamId {
            params["teamId"] = teamId
        }

        return WmcMediaRequest.requestData(path: "/wmc/v1/albumClassify/list", parameters: params) { request in
            request.method = .post
        }.parseListModelWithClass(WmcMediaAlbumModel.self)
    }
    
    /// 分类相册成员列表
    /// - DocumentUrl: http://yapi.3pvr.com/project/219/interface/api/3076
    /// - Parameters:
    ///   - albumId: 相册id
    ///   - busId: 业务id 团队相册为团队id 个人相册为鱼泡id
    ///   - currentPage: 页码
    ///   - pageSize: 展示条数
    ///   - teamId: 团队id
    /// - Returns: Observable<WmcMediaClassifyAlbumMemberModel>
    static func fetchClassifyAlbumMemberList(albumId: Int, busId: String, currentPage: Int, pageSize: Int, teamId: Int) -> Observable<[WmcMediaClassifyAlbumMemberModel]> {
        let params: [String: Any] = ["albumId": albumId, "busId": busId, "currentPage": currentPage, "pageSize": pageSize, "teamId": teamId]

        
        return WmcMediaRequest.requestData(path: "/wmc/v1/albumClassify/getAlbumMemberList", parameters: params) { request in
            request.method = .post
        }.parseListModelWithClass(WmcMediaClassifyAlbumMemberModel.self,designatedPath: "data")
    }
    
    /// 相册同步资源
    /// - DocumentUrl: http://yapi.3pvr.com/project/219/interface/api/3086
    /// - Parameters:
    ///   - resourceIds: 资源id集合
    ///   - albumBasicsInfo: 同步相册基本信息
    ///   - busId: 业务id 团队相册为团队id 个人相册为鱼泡id
    /// - Returns: Observable<YPNetworkResponse>
    static func syncResource(resourceIds: [String], albumBasicsInfo: [String: Any]) -> Observable<YPNetworkResponse> {
        var params: [String: Any] = albumBasicsInfo
        params["resourceIds"] = resourceIds
        if let userId = WmcLoginManager.shared.yupaoAccountModel?.userId {
            params["busId"] = userId
        }
        return WmcMediaRequest.requestData(path: "/wmc/v1/teamAlbum/syncResource", parameters: params) { request in
            request.method = .post
        }
    }
    
    
    /// 日历获取拍照的日期
    /// - DocumentUrl: http://rap2.taobao.org/organization/repository/editor?id=302804&mod=511883&itf=2270078
    /// - Parameters:
    ///   - albumType: 相册类型，0：个人，1团队
    ///   - businessId:团队ID
    ///   - startDate: 开始时间 yyyy-MM-dd格式
    ///   - endDate: 结束时间 yyyy-MM-dd格式
    ///   - resourceType: 资源类型 0：图片  1：视频   不传：所有
    /// - Returns: Observable<[WmcMediaTakePhotoDayModel]>
    static func fetchTakePhotoCalendar(busId: String,startDate: String, endDate: String, resourceType: [Int]? = nil) -> Observable<[WmcMediaTakePhotoDayModel]> {
        
        var params: [String: Any] = ["albumType": 1,"busId": busId, "startDate": startDate, "endDate": endDate]
        if let resourceType = resourceType {
            params["resourceType"] = resourceType
        }
        return WmcMediaRequest.requestData(path: "/media/v1/album/getTakeDate", parameters: params) { request in
            request.method = .post
        }.parseListModelWithClass(WmcMediaTakePhotoDayModel.self, designatedPath: "takeDateList")
    }
}

extension WmcMediaTeamAlbumAPI {
    
    //团队成员个人相册操作
    
    /// 查询相册
    /// - DocumentUrl: http://rap2.taobao.org/organization/repository/editor?id=302804&mod=511883&itf=2269190
    /// - Parameters:
    ///   - albumType: 相册类型，0：个人
    ///   - businessId: 业务id，albumType为0时是鱼泡用户id
    ///   - page: 当前页号
    ///   - pageSize: 分页大小
    ///   - busId 跟团队ID一样
    ///   - wmIds: 水印类型id
    ///   - date: yyyy-MM-dd格式
    ///   - resourceType: 资源类型 0：图片  1：视频   不传：所有
    /// - Returns: Observable<WmcMediaPhotoModel>
    
    static func fetchPhotoList(page: Int, pageSize: Int,busId: Int, wmIds: [Int]? = nil,uid: Int? = nil, startDate: String? = nil, endDate: String? = nil, resourceType: [Int]? = nil) -> Observable<[WmcMediaPhotoModel]> {
        var params: [String: Any] = ["albumType": 1, "currentPage": page, "pageSize": pageSize]
        
        params["busId"] = busId
        
        if let wmIds = wmIds {
            params["wmIds"] = wmIds
        }

        if let uid = uid {
            params["uid"] = uid
        }
        
        if let startDate = startDate {
            params["startDate"] = startDate
        }
        
        if let endDate = endDate {
            params["endDate"] = endDate
        }
        
        if let resourceType = resourceType {
            params["resourceType"] = resourceType
        }
        
        return WmcMediaRequest.requestData(path: "/media/v1/album/list", parameters: params) { request in
            request.method = .post
        }.parseListModelWithClass(WmcMediaPhotoModel.self, designatedPath: "data")
    }
    
    
    static func fetchPhotoList(page: Int, pageSize: Int,busId: Int, wmIds: [Int]? = nil,uid: Int? = nil, startDate: String? = nil, endDate: String? = nil, resourceType: [Int]? = nil, success: @escaping ((list: [WmcMediaPhotoModel], total: Int)) -> Void, failure: @escaping YPNetworkRequestFailure) {
        var params: [String: Any] = ["albumType": 1, "currentPage": page, "pageSize": pageSize]
        
        params["busId"] = busId
        
        
        if let wmIds = wmIds {
            params["wmIds"] = wmIds
        }
        
        if let uid = uid {
            params["uid"] = uid
        }
        
        if let startDate = startDate {
            params["startDate"] = startDate
        }
        
        if let endDate = endDate {
            params["endDate"] = endDate
        }
        
        if let resourceType = resourceType {
            params["resourceType"] = resourceType
        }
        
        WmcMediaRequest.requestData(path: "/media/v1/album/list", parameters: params) { request in
            request.method = .post
        } success: { (response: YPNetworkResponse) in
            if let dic = response.data as? [String: Any],
               let list = dic["data"] as? [Any],
               let paginator = dic["paginator"] as? [String: Any]
            {
                let listArray = list.parseListModelWithClass(WmcMediaPhotoModel.self)
                let total = (paginator["totalRecord"] as? Int) ?? 0
                let result = (list: listArray, total: total)
                success(result)
            } else {
                failure(.init(code: 0, message: "参数解析失败！"))
            }
        } failure: { error in
            failure(error)
        }
    }

       /// 删除照片
       /// - DocumentUrl: http://rap2.taobao.org/organization/repository/editor?id=302804&mod=511883&itf=2269186
       /// - Parameters:
       ///   - albumType: 相册类型，0：个人
       ///   - businessId: 资源id，albumType为0时是鱼泡用户id
       ///   - id: 相册资源id
       /// - Returns:  Observable<YPNetworkResponse>
       static func deletePhoto(id: Int,busId: Int) -> Observable<YPNetworkResponse> {
           var params: [String: Any] = ["albumType": 1, "id": id]
           
           params["busId"] = busId


           return WmcMediaRequest.requestData(path: "/media/v1/album/delete", parameters: params) { request in
               request.method = .post
           }
       }

       /// 日历获取拍照的日期
       /// - DocumentUrl: http://rap2.taobao.org/organization/repository/editor?id=302804&mod=511883&itf=2270078
       /// - Parameters:
       ///   - albumType: 相册类型，0：个人
       ///   - businessId: 资源id，albumType为0时是鱼泡用户id
       ///   - startDate: 开始时间 yyyy-MM-dd格式
       ///   - endDate: 结束时间 yyyy-MM-dd格式
       ///   - resourceType: 资源类型 0：图片  1：视频   不传：所有
       /// - Returns: Observable<[WmcMediaTakePhotoDayModel]>
       static func fetchTakePhotoCalendar(busId: Int, startDate: String, endDate: String, resourceType: [Int]? = nil) -> Observable<[WmcMediaTakePhotoDayModel]> {
           var params: [String: Any] = ["albumType": 1, "startDate": startDate, "endDate": endDate]

           params["busId"] = busId

           if let resourceType = resourceType {
               params["resourceType"] = resourceType
           }

           return WmcMediaRequest.requestData(path: "/media/v1/album/getTakeDate", parameters: params) { request in
               request.method = .post
           }.parseListModelWithClass(WmcMediaTakePhotoDayModel.self, designatedPath: "takeDateList")
       }

       /// 分类照片查询
       /// DocumentUrl: http://rap2.taobao.org/organization/repository/editor?id=302804&mod=511883&itf=2270108
       /// - Parameters:
       ///   - albumType: 相册类型，0：个人
       ///   - businessId: 资源id，albumType为0时是鱼泡用户id
       ///   - resourceType: 资源类型 0：图片  1：视频   不传：所有
       /// - Returns: Observable<[WmcMediaAlbumModel]>
       static func fetchAlbumList(busId: Int, resourceType: [Int]? = nil) -> Observable<[WmcMediaAlbumModel]> {
           var params: [String: Any] = ["albumType": 1]

           params["busId"] = busId


           if let resourceType = resourceType {
               params["resourceType"] = resourceType
           }

           return WmcMediaRequest.requestData(path: "/media/v1/album/classifyList", parameters: params) { request in
               request.method = .post
           }.parseListModelWithClass(WmcMediaAlbumModel.self, designatedPath: "allResources")
       }

       /// 水印分类获取查询
       /// - DocumentUrl: http://rap2.taobao.org/organization/repository/editor?id=302804&mod=511883&itf=2270164
       /// - Parameters:
       ///   - albumType: 相册类型，0：个人
       ///   - businessId: 资源id，albumType为0时是鱼泡用户id
       ///   - resourceType: 资源类型 0：图片  1：视频   不传：所有
       /// - Returns: Observable<[WmcMediaPhotographStatisticsModel]>
       static func fetchTakePhotoStatistics(busId: Int,resourceType: [Int]? = nil) -> Observable<[WmcMediaPhotographStatisticsModel]> {
           var params: [String: Any] = ["albumType": 1]
           
           params["busId"] = busId


           if let resourceType = resourceType {
               params["resourceType"] = resourceType
           }

           return WmcMediaRequest.requestData(path: "/media/v1/album/watermarkList", parameters: params) { request in
               request.method = .post
           }.parseListModelWithClass(WmcMediaPhotographStatisticsModel.self, designatedPath: "albumWaterMarkList")
       }

       /// 获取指定时间以前最近一次拍照时间
       /// - Parameters:
       ///   - albumType: 相册类型，0：个人
       ///   - businessId: 资源id，albumType为0时是鱼泡用户id
       ///   - maxDate: 最大时间
       ///   - resourceType: 资源类型 0：图片  1：视频   不传：所有
       /// - Returns: Observable<YPNetworkResponse>
       static func fetchTakePhotoLastDate(albumType: Int, businessId: String, maxDate: String, resourceType: [Int]? = nil) -> Observable<YPNetworkResponse> {
           var params: [String: Any] = ["albumType": albumType, "busId": businessId, "maxDate": maxDate]

           if let resourceType = resourceType {
               params["resourceType"] = resourceType
           }

           return WmcMediaRequest.requestData(path: "/media/v1/album/getPreDate", parameters: params) { request in
               request.method = .post
           }
       }
    
    static func fetchAlbumShareInfo(busId: Int, uid: Int? = nil ,wmIds: [Int]? = nil) -> Observable<WmcMediaShareModel> {
        var params: [String: Any] = ["albumType": 1]
        
        params["busId"] = busId
        
        if let uid = uid {
            params["uid"] = uid
        }
        
        if let wmIds = wmIds {
            params["wmIds"] = wmIds
        }
        
        return WmcMediaRequest.requestData(path: "/media/v1/album/shareAlbum", parameters: params) { request in
            request.method = .post
        }.parseModelWithClass(WmcMediaShareModel.self)
    }
    
    static func fetchVideoShareInfo(busId: Int ,id: Int) -> Observable<WmcMediaShareModel> {
        var params: [String: Any] = ["albumType": 1, "albumResourceId": id]
        
        if let userId = WmcLoginManager.shared.yupaoAccountModel?.userId {
            params["busId"] = busId
            params["shareUid"] = userId
        }
        
        return WmcMediaRequest.requestData(path: "/media/v1/album/shareVideo", parameters: params) { request in
            request.method = .post
        }.parseModelWithClass(WmcMediaShareModel.self)
    }
}

extension WmcMediaTeamAlbumAPI {
    
}
